package com.jiuzhou.surery;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.jiuzhou.surery.security.SurveyUserDetails;
import com.jiuzhou.surery.security.SurveyUserDetailsManager;
import com.jiuzhou.surery.service.StorageService;

@SpringBootApplication
public class SureryApplication {

	public static void main(String[] args) {
		SpringApplication.run(SureryApplication.class, args);
	}

	@Bean
	CommandLineRunner init(StorageService storageService) {
		return (args) -> {
			storageService.deleteAll();
			storageService.init();
		};
	}




	@Bean
	CommandLineRunner createUsers(SurveyUserDetailsManager userDetailsManager, PasswordEncoder passwordEncoder) {
		return (args) -> {
			if(!userDetailsManager.userExists("admin")) {
				userDetailsManager.createUser(new SurveyUserDetails("admin", 
				passwordEncoder.encode("admin"), 
				true, 
				"火车1",
				"ADMIN"));
			}
			if (!userDetailsManager.userExists("user")) {
				userDetailsManager.createUser(new SurveyUserDetails("user", 
				passwordEncoder.encode("user"), 
				true, 
				"火车2",
				"USER"));
			}
		};
	}
}
